import { createSlice } from "@reduxjs/toolkit";
import Api from "@/api";

export const userStore = createSlice({
  name: "UserStore",
  initialState: {
    token: "",
    userInfo: {},
    roles: [],
    permissions: [],
  },
  reducers: {
    setToken: (state, action) => {
      state.token = action.payload;
    },
    setUserInfo: (state, action) => {
      state.userInfo = action.payload;
    },
    setRoles: (state, action) => {
      state.roles = action.payload;
    },
    setPermissions: (state, action) => {
      state.roles = action.payload;
    },
  },
});
const storeLogin = (data) => {
  return async (dispatch) => {
    Api.Login({ ...data }).then((res) => {
      if (res.code === 200) {
        dispatch(setToken(res.data.access_token));
      }
    });
  };
};
const { setToken, setUserInfo, setRoles, setPermissions } = userStore.actions;

const reducer = userStore.reducer;
export { setToken, setUserInfo, setRoles, setPermissions, storeLogin };
export default reducer;
